from django.shortcuts import render
from django.http.response import HttpResponse
from django.template.context_processors import request
from django.views.decorators.http import require_http_methods
from pkg_resources import require
from django.http import QueryDict
from django.http import JsonResponse

import json,os


# Create your views here.

def index(request):
    print('app视图运行成功')
    return HttpResponse('网页运行成功')

@require_http_methods(['POST'])
def login(request):
    print('login视图运行成功')
    return HttpResponse('login成功')

def login2(request):
    print('成功')
    return HttpResponse('login2成功')

def index2(request):
    print('index2运行成功')
    return HttpResponse('index2运行成功')

def index3(request):
    """响应html格式"""
    return HttpResponse(content="<h1>HTML代码</h1>", content_type="text/html", status=201, headers={"toke": 123})

def index4(request):
    """响应json格式"""
    # data = {
    #     "id" : 1,
    #     "title" : "生存法则",
    #     "content" : "如何在当今社会活的舒服",
    #     "price" : 69.9
    # }
    # json_data = json.dumps(data)

    data = [
        {"id":1,"name":"小李","age":12},
        {"id":2,"name":"小王","age":14},
    ]
    json_data = json.dumps(data)
    return HttpResponse(content=json_data, content_type="text/json")

def index5(request):
    data = [
        {"id":1,"title":"生存法则","content":"如何在当今社会实现财富自由","price":89.9},
        {"id":2,"title":"生存法则","content":"如何在当今社会颐养晚年","price":99.9},
    ]
    return JsonResponse(data, safe=False)

def index6(request):
    """返回图片格式"""
    with open("./wchat.png", "rb") as f:
        img = f.read()
    return HttpResponse(content=img, content_type="image/png")

def index7(request):
    """自定义响应格式"""
    response = HttpResponse("ok!!!")
    response["company"] = "baidu"
    return response